package com.tjetc.usermanage;

import com.tjetc.domian.Resident;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.ResidentService;
import com.tjetc.vo.Result;

import java.util.List;
import java.util.Scanner;


public class ResidenteView {
    private Scanner scanner = new Scanner(System.in);
    private ResidentService residentService = ClientProxy.getProxyInstance(ResidentService.class);
    public void start() {
        while (true) {
            System.out.println("居民信息菜单");
            System.out.println("1.查询居民信息");
            System.out.println("2.修改居民信息");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i){
                case 1:
                    System.out.println("查询居民信息");
                    select();
                    break;
                case 2:
                    System.out.println("修改居民信息");
                    update();
                    break;
                case 0:
                    return;
            }
        }
    }

    private void update() {
        System.out.println("请输入要修改的id");
        int id = scanner.nextInt();
        System.out.println("请输入要修改的姓名");
        String name = scanner.next();
        System.out.println("请输入要修改的性别");
        String gender = scanner.next();
        System.out.println("请输入要修改的年龄");
        int age = scanner.nextInt();
        System.out.println("请输入要修改的公寓id");
        int apartmentid = scanner.nextInt();
        String contact = null;
        while (true) {
            System.out.println("请输入要修改的电话");
            contact = scanner.next();
            if (!contact.matches("1[35789]\\d{9}")){
                System.out.println("请输入正确的手机号码");

            }else {
                break;
            }
        }
        System.out.println("请输入要修改的邮箱");
        String email = scanner.next();
        Resident resident = new Resident();
        resident.setResidentid( id);
        resident.setName(name);
        resident.setGender(gender);
        resident.setAge(age);
        resident.setApartmentid(apartmentid);
        resident.setContact(contact);
        resident.setEmail(email);
        Result result = residentService.update(resident);
        System.out.println(result.getMsg());
    }

    private void select() {
        List<Resident> list = residentService.select();
        if (list != null){
            for (Resident resident : list) {
                System.out.println(resident);
            }
        }else {
            System.out.println("没有数据");
        }
    }
}
